var MyThroughputRule;
/*
function saveAs(name, data, type) {
    const element = document.createElementNS("http://www.w3.org/1999/xhtml", "a"),
    ev = document.createEvent("MouseEvents"),
    urlObject = window.URL || window.webkitURL || window,
    export_blob = new Blob([data], {type: type ? type : ''});
    element.href = urlObject.createObjectURL(export_blob);
    element.download = name;
    ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    element.dispatchEvent(ev);
    urlObject.revokeObjectURL(element.href);
    element.href = '';
}
function download(){
    var appElement = document.querySelector('[ng-controller=DashController]');
    var $scope = angular.element(appElement).scope();
    //
    var Quality = $scope.playerDownloadingQualitybytext ;  // Data from monitor
    var Buffer = $scope.playerBufferLengthbytext ;  // Data from monitor
    var Throughput = $scope.playerAverageThroughputbytext ;  // Data from monitor
    var pause_flag = $scope.pause_flagbytext ;  // Data from monitor
    var pause_total = $scope.pause_totalbytext ;  // Data from monitor
    var rebuffer_time = $scope.rebuffer_timebytext ;  // Data from monitor

    saveAs("bitrate",Quality, 1);
    saveAs("buffer",Buffer, 1);
    saveAs("throughput",Throughput, 1);
    saveAs("pause_flag",pause_flag, 1);
    saveAs("pause_total",pause_total, 1);
    saveAs("rebuffer_time",rebuffer_time, 1);
}
 */
function MyThroughputRuleClass() {
    var appElement = document.querySelector('[ng-controller=DashController]');
    var $scope = angular.element(appElement).scope();


    let factory = dashjs.FactoryMaker;
    let SwitchRequest = factory.getClassFactoryByName('SwitchRequest');
    const context = this.context;
    let instance;
    //

    function setup() {
    }
    function writeObj(obj){
        var description = "";
        for(var i in obj){
        var property=obj[i];
        description+=i+" = "+property+"\n";
        }
        console.log(description);
    }

    // var funDownload = function (content, filename) {    // 创建隐藏的可***链接
    //     var eleLink = document.createElement('a');
    //     eleLink.download = filename;
    //     eleLink.style.display = 'none';    // 字符内容转变成blob地址
    //     var blob = new Blob([content]);
    //     eleLink.href = URL.createObjectURL(blob);    // 触发点击
    //     document.body.appendChild(eleLink);
    //     eleLink.click();    // 然后移除
    //     document.body.removeChild(eleLink);
    //  };



    function getMaxIndex(rulesContext) {
        const switchRequest = SwitchRequest(context).create();

        if (!rulesContext || !rulesContext.hasOwnProperty('getMediaInfo') || !rulesContext.hasOwnProperty('getMediaType') || !rulesContext.hasOwnProperty('useBufferOccupancyABR') ||
            !rulesContext.hasOwnProperty('getAbrController') || !rulesContext.hasOwnProperty('getScheduleController')) {
            return switchRequest;
        }

        const mediaInfo = rulesContext.getMediaInfo();
        const mediaType = rulesContext.getMediaType();
        const scheduleController = rulesContext.getScheduleController();
        const abrController = rulesContext.getAbrController();
        const streamInfo = rulesContext.getStreamInfo();
        const isDynamic = streamInfo && streamInfo.manifestInfo ? streamInfo.manifestInfo.isDynamic : null;
        const throughputHistory = abrController.getThroughputHistory();
        // 整体tiles的数据
        console.log("整体tiles的数据")
        //当前码率变化
        var playerQualityLength = $scope.playerDownloadingQuality
        console.log("当前码率变化"+playerQualityLength)
        //当前buffer变化
        var playerBufferLength = $scope.playerBufferLength
        console.log("当前buffer变化"+playerBufferLength)
        //当前throughgput变化
        var playerAverageThroughput = $scope.playerAverageThroughput
        console.log("当前throughgput变化"+playerAverageThroughput)
        //当前是否卡顿
        var pause_flag = $scope.pause_flag
        console.log("当前是否卡顿"+pause_flag)
        //卡顿总次数
        var pause_total = $scope.pause_total
        console.log("卡顿总次数"+pause_total)
        //卡顿总时长
        var rebuffer_time = $scope.rebuffer_time
        console.log("卡顿总时长"+rebuffer_time)

        // 目前tile的可选码率列表
        const BitrateList = abrController.getBitrateList(mediaInfo);
        // 目前tile的码率索引
        const bitrate = abrController.getQualityFor("video")
        const throughput = throughputHistory.getSafeAverageThroughput(mediaType, isDynamic);
        // console.log("throughputHistory是什么？："+writeObj(abrController))
        console.log("BitrateList是什么？："+bitrate)
        const latency = throughputHistory.getAverageLatency(mediaType);
        const useBufferOccupancyABR = rulesContext.useBufferOccupancyABR();

        if (isNaN(throughput) || useBufferOccupancyABR) {
            return switchRequest;
        }

        switchRequest.quality = abrController.getQualityForBitrate(mediaInfo, throughput, latency);
        scheduleController.setTimeToLoadDelay(0);
        switchRequest.reason = {throughput: throughput, latency: latency};

        return switchRequest;
    }

    instance = {
        getMaxIndex: getMaxIndex,
    };

    setup();

    return instance;
}


MyThroughputRuleClass.__dashjs_factory_name = 'MyThroughputRule';
MyThroughputRule = dashjs.FactoryMaker.getClassFactory(MyThroughputRuleClass);
